#include "statemachine.h"

extern const wxEventType wxEVT_COMMAND_SHAPE_SELECT_CHANGED;

IMPLEMENT_DYNAMIC_CLASS(wxShapeSelectChangeEvent, wxNotifyEvent)

BEGIN_EVENT_TABLE(StateMachine, wxShapeCanvas)
    EVT_PAINT(StateMachine::OnPaint)
END_EVENT_TABLE()

void StateMachine::OnSelectShape(wxShape*s){
    wxShapeSelectChangeEvent event(s, wxEVT_COMMAND_SHAPE_SELECT_CHANGED, GetId());
    event.SetEventObject(this);
    GetEventHandler()->ProcessEvent(event);
}

void StateMachine::OnPaint(wxPaintEvent& event)
{
//  if (GetDiagram())
    wxShapeCanvas::OnPaint(event);
}

void MyEvtHandler::OnEndSize(double WXUNUSED(x), double WXUNUSED(y))
{
  wxClientDC dc(GetShape()->GetCanvas());
  GetShape()->GetCanvas()->PrepareDC(dc);

  GetShape()->FormatText(dc, /* (char*) (const char*) */ label);
}

void MyEvtHandler::OnLeftClick(double WXUNUSED(x), double WXUNUSED(y), int keys, int WXUNUSED(attachment))
{
  wxClientDC dc(GetShape()->GetCanvas());
  GetShape()->GetCanvas()->PrepareDC(dc);

  if (keys == 0)
  {
    // Selection is a concept the library knows about
    if (GetShape()->Selected())
    {
      GetShape()->Select(false, &dc);
      ((StateMachine*)GetShape()->GetCanvas())->OnSelectShape(NULL);
      GetShape()->GetCanvas()->Redraw(dc); // Redraw because bits of objects will be are missing
    }
    else
    {
      // Ensure no other shape is selected, to simplify Undo/Redo code
      bool redraw = false;
      wxObjectList::compatibility_iterator node = GetShape()->GetCanvas()->GetDiagram()->GetShapeList()->GetFirst();
      while (node)
      {
        wxShape *eachShape = (wxShape *)node->GetData();
        if (eachShape->GetParent() == NULL)
        {
          if (eachShape->Selected())
          {
            eachShape->Select(false, &dc);
            redraw = true;
          }
        }
        node = node->GetNext();
      }
      GetShape()->Select(true, &dc);
      ((StateMachine*)GetShape()->GetCanvas())->OnSelectShape(GetShape());
      if (redraw)
        GetShape()->GetCanvas()->Redraw(dc);
    }
  }
  else if (keys & KEY_CTRL)
  {
    // Do something for CONTROL
  }
  else
  {
  }
}

